import { ApiCall } from "tsrpc";
import { ReqTick, ResTick } from "../../../shared/master/body/PtlTick";
import { player } from "../../../model/fight/body/player";
import { Tpl_msgMiss, template } from "../../../shared/master/MsgAction";
import { npc } from "../../../model/fight/body/npc";

export default async function (call: ApiCall<ReqTick, ResTick>) {
    call.succ({})
    let user = call.req._player as player;
    let msg = call.req.msg;
    // 分析消息是否为放技能
    let sks = [...user.getAllSkills()];
    let _idx = sks.findIndex((item) => {
        return item.rename == msg;
    })
    let miss = true;
    if (_idx != -1) {
        // 释放技能
        user.callApi('body/FreeSkill', {
            skName: msg
        })
        return;
    }

    // 分析是否为查看宠物
    let pets = user.getPets();
    _idx = pets.findIndex((item) => {
        return item.name == msg;
    })
    if (_idx != -1) {
        user.callApi('body/pet/Look', { idx: _idx + 1 })
        return;
    }


    // 分析是否为使用指定功能
    let _cell =  user.getPosition();
    let bodys = _cell.getBodys()
    bodys.forEach(element => {
        if(element instanceof npc && element.name == call.req.msg){
            element.tick(user);
            miss = false;
        }
    });

    if(miss){
        let tpl:Tpl_msgMiss = {str:msg}
        user.sendMsg(template.Msg_miss,tpl)
    }
}